// pages/assets/assets.js
let common = require("../../utils/formatCurrency");
const app = getApp();
import * as echarts from '../../ec-canvas/echarts';

Page({
  data: {
    ec: {
      lazyLoad: true
    },
    chartOrform: true,
    showButton: true,
    fixedText: '列表',
    currentAssets: [{
      "asset": "货币资金",
      "assetRow": "1",
      "endBalance": "164863",
      "initBalance": "5521",
      "id": "1",
      "assetText": "科目：库存现金+银行存款+其他货币资金"
    }, {
      "asset": "短期投资",
      "assetRow": "2",
      "endBalance": "15516",
      "initBalance": "6244",
      "id": "2",
      "assetText": "科目：短期投资"
    }, {
      "asset": "应收票据",
      "assetRow": "3",
      "endBalance": "486",
      "initBalance": "855",
      "id": "3",
      "assetText": "科目：应收票据"
    }, {
      "asset": "应收账款",
      "assetRow": "4",
      "endBalance": "45646",
      "initBalance": "15",
      "id": "4",
      "assetText": "科目：应收账款+预收账款"
    }, {
      "asset": "预付账款",
      "assetRow": "5",
      "endBalance": "430",
      "initBalance": "60",
      "id": "5",
      "assetText": "科目：预付账款+应付账款"
    }, {
      "asset": "应收股利",
      "assetRow": "6",
      "endBalance": "66",
      "initBalance": "526",
      "id": "6",
      "assetText": "科目：应收股利"
    }, {
      "asset": "应收利息",
      "assetRow": "7",
      "endBalance": "1665",
      "initBalance": "321",
      "id": "7",
      "assetText": "科目：应收利息"
    }, {
      "asset": "其他应收款",
      "assetRow": "8",
      "endBalance": "-4966",
      "initBalance": "-626",
      "id": "8",
      "assetText": "科目：其他应收款"
    }, {
      "asset": "存货",
      "assetRow": "9",
      "endBalance": "-46",
      "initBalance": "6",
      "id": "9",
      "assetText": "科目：材料采购+在途物资+原材料+材料成本差异+库存商品-商品进销差价+委assetText资产+生产成本+制造费用+工程施工+机械作业"
    }, {
      "asset": "其中：原材料",
      "assetRow": "10",
      "endBalance": "-9556",
      "initBalance": "2566",
      "id": "10",
      "assetText": "科目：原材料"
    }, {
      "asset": "在产品",
      "assetRow": "11",
      "endBalance": "65662",
      "initBalance": "66",
      "id": "11",
      "assetText": "科目：生产成本+制造费用+工程施工+机械作业"
    }, {
      "asset": "库存商品",
      "assetRow": "12",
      "endBalance": "5995",
      "initBalance": "-65",
      "id": "12",
      "assetText": "科目：库存商品"
    }, {
      "asset": "周转材料",
      "assetRow": "13",
      "endBalance": "-456",
      "initBalance": "965",
      "id": "13",
      "assetText": "科目：周转材料"
    }, {
      "asset": "其他流动资产",
      "assetRow": "14",
      "endBalance": "46",
      "initBalance": "56",
      "id": "14",
      "assetText": "科目：待处理流动资产损益"
    }, {
      "asset": "流动资产合计",
      "assetRow": "15",
      "endBalance": "466",
      "initBalance": "65",
      "id": "15",
      "assetText": "行次：行1+行2+行3+行4+行5+行6+行7+行8+行9+行14"
    }],
    nonCurrentAssets: [{
      "asset": "长期债券投资",
      "assetRow": "1",
      "endBalance": "0",
      "initBalance": "0",
      "id": "1",
      "assetText": "科目：长期债券投资"
    }, {
      "asset": "长期股权投资",
      "assetRow": "2",
      "endBalance": "0",
      "initBalance": "0",
      "id": "2",
      "assetText": "科目：长期股权投资"
    }, {
      "asset": "固定资产原价",
      "assetRow": "3",
      "endBalance": "0",
      "initBalance": "0",
      "id": "3",
      "assetText": "科目：固定资产",
    }, {
      "asset": "减：累计折旧",
      "assetRow": "4",
      "endBalance": "0",
      "initBalance": "0",
      "id": "4",
      "assetText": "科目：累计折旧"
    }, {
      "asset": "固定资产账面价值",
      "assetRow": "5",
      "endBalance": "0",
      "initBalance": "0",
      "id": "5",
      "assetText": "行次：行18-行19",
    }, {
      "asset": "在建工程",
      "assetRow": "6",
      "endBalance": "0",
      "initBalance": "0",
      "id": "6",
      "assetText": "科目：在建工程",
    }, {
      "asset": "工程物资",
      "assetRow": "7",
      "endBalance": "0",
      "initBalance": "0",
      "id": "7",
      "assetText": "科目：工程物资"
    }, {
      "asset": "固定资产清理",
      "assetRow": "8",
      "endBalance": "0",
      "initBalance": "0",
      "id": "8",
      "assetText": "科目：固定资产清理"
    }, {
      "asset": "生产性生物资产",
      "assetRow": "9",
      "endBalance": "0",
      "initBalance": "0",
      "id": "9",
      "assetText": "科目：生产性生物资产-生产性生物资产累计折旧"
    }, {
      "asset": "无形资产",
      "assetRow": "10",
      "endBalance": "0",
      "initBalance": "0",
      "id": "10",
      "assetText": "科目：无形资产-累计摊销"
    }, {
      "asset": "开发支出",
      "assetRow": "11",
      "endBalance": "0",
      "initBalance": "0",
      "id": "11",
      "assetText": "科目：研发支出",
    }, {
      "asset": "长期待摊费用",
      "assetRow": "12",
      "endBalance": "0",
      "initBalance": "0",
      "id": "12",
      "assetText": "科目：长期待摊费用"
    }, {
      "asset": "其他非流动资产",
      "assetRow": "13",
      "endBalance": "0",
      "initBalance": "0",
      "id": "13",
      "assetText": "科目：待处理非流动资产损益"
    }, {
      "asset": "非流动资产合计",
      "assetRow": "14",
      "endBalance": "0",
      "initBalance": "0",
      "id": "14",
      "assetText": "行次：行16+行17+行20+行21+行22+行23+行24+行25+行26+行27+行28"
    }, {
      "asset": "资产总计",
      "assetRow": "15",
      "endBalance": "0",
      "initBalance": "0",
      "id": "15",
      "assetText": "行次：行15+行29"
    }],
    currentLiabilities: [{
      "asset": "短期借款",
      "assetRow": "1",
      "endBalance": "0",
      "initBalance": "0",
      "id": "1",
      "assetText": "科目：短期借款",
    }, {
      "asset": "应付票据",
      "assetRow": "2",
      "endBalance": "0",
      "initBalance": "0",
      "id": "2",
      "assetText": "科目：应付票据"
    }, {
      "asset": "应付账款",
      "assetRow": "3",
      "endBalance": "0",
      "initBalance": "0",
      "id": "3",
      "assetText": "科目：应付账款+预付账款",
    }, {
      "assetRow": "4",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "预收账款",
      "id": "4",
      "assetText": "科目：应收账款+预收账款",
    }, {
      "assetRow": "5",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "应付职工薪酬",
      "id": "5",
      "assetText": "科目：应付职工薪酬",
    }, {
      "assetRow": "6",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "应交税费",
      "id": "6",
      "assetText": "科目：应交税费",
    }, {
      "assetRow": "7",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "应付利息",
      "id": "7",
      "assetText": "科目：应付利息",
    }, {
      "assetRow": "8",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "应付利润",
      "id": "8",
      "assetText": "科目：应付利润",
    }, {
      "assetRow": "9",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "其他应付款",
      "id": "9",
      "assistantNo": null
    }, {
      "assetRow": "14",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "其他流动负债",
      "id": "10",
      "assetText": null,
    }, {
      "assetRow": "15",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "流动负债合计",
      "id": "11",
      "assetText": "行次：行31+行32+行33+行34+行35+行36+行37+行38+行39+行40",
    }],
    nonCurrentLiabilities: [{
      "assetRow": "1",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "长期借款",
      "id": "1",
      "assetText": "科目：长期借款",
    }, {
      "assetRow": "2",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "长期应付款",
      "id": "2",
      "assetText": "科目：长期应付款",
    }, {
      "assetRow": "3",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "递延收益",
      "id": "3",
      "assetText": "科目：递延收益",
    }, {
      "assetRow": "4",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "其他非流动负债",
      "id": "4",
      "assetText": null,
    }, {
      "assetRow": "5",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "非流动负债合计",
      "id": "5",
      "assetText": "行次：行42+行43+行44+行45",
    }, {
      "assetRow": "6",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "负债合计",
      "id": "6",
      "assetText": "行次：行41+行46",
    }],
    ownerasset: [{
      "assetRow": "1",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "实收资本（或股本）",
      "id": "1",
      "assetText": "科目：实收资本",
    }, {
      "assetRow": "2",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "资本公积",
      "id": "2",
      "assetText": "科目：资本公积",
    }, {
      "assetRow": "3",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "盈余公积",
      "id": "3",
      "assetText": "科目：盈余公积",
    }, {
      "assetRow": "4",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "未分配利润",
      "id": "4",
      "assetText": "科目：利润分配+本年利润",
    }, {
      "assetRow": "5",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "所有者权益（或股东权益）合计",
      "id": "5",
      "assetText": "行次：行48+行49+行50+行51",
    }, {
      "assetRow": "6",
      "endBalance": "0",
      "initBalance": "0",
      "asset": "负债和所有者权益（或股东权益）总计",
      "id": "6",
      "assetText": "行次：行47+行52",
    }],


    chartList:[
      {
        asset:'流动资产：',
        balance: '',
        lock: false,
        items:[
          { asset: '库存现金', balance: '5220'},
          { asset: '银行存款', balance: '219250' },
          { asset: '其他货币资金', balance: '0' }
        ]
      },{
        asset: '非流动资产：',
        balance: '',
        lock: false,
        items: [
          { asset: '应收账款', balance: '5220' },
          { asset: '预收账款', balance: '219250' }
        ]
      }, {
        asset: '流动负债：',
        balance: '',
        lock: false,
        items: [
          { asset: '预付账款', balance: '5220' },
          { asset: '应付账款', balance: '219250' }
        ]
      },{
        asset: '非流动负债：',
        balance: '',
        lock: false,
        items: [
          { asset: '材料采购', balance: '5220' },
          { asset: '在途物资', balance: '219250' },
          { asset: '原材料', balance: '219250' },
          { asset: '材料成本差异', balance: '219250' },
          { asset: '在途物资', balance: '219250' },
          { asset: '在途物资', balance: '219250' },
        ]
      }, {
        asset: '所有者权益（或股东权益）',
        balance: '',
        lock: false,
        items: [
          { asset: '固定资产', balance: '5220' },
          { asset: '累计折旧', balance: '219250' }
        ]
      }
    ],
    height: '380rpx',
    ownerTotal: 0,
    ldfzTotal: 0,
    fldfzTotal: 0,
    optionData:[],
    date:''
  },

  onLoad: function (options) {
    common.handleNavigationBar(app.globalData.company)
    wx.showLoading({
      title: '加载中',
    })
    this.data.date = options.data;
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/balancesheet/find',
      method: 'POST',
      header: {
        token: app.globalData.token,
        version: app.globalData.version
      },
      data: {
        accountBookId: app.globalData.accountBookId,
        endPeriod: options.data
      },
      success: function (res) {
        that.handleData(res.data)
      }
    }) 
  },


  handleData(data){
    //数据处理
    let length1 = this.data.currentAssets.length;
    let length2 = this.data.currentLiabilities.length;
    let length3 = this.data.nonCurrentLiabilities.length;
    this.data.currentAssets.forEach((item,i) => {
      item.endBalance = Number(data.assetEndBalances[i]);
      item.initBalance = Number(data.assetInitBalances[i]);
    })
    this.data.nonCurrentAssets.forEach((item,i) => {
      item.endBalance = Number(data.assetEndBalances[i + length1]);
      item.initBalance = Number(data.assetInitBalances[i + length1]);
    })
    this.data.currentLiabilities.forEach((item,i) => {
      item.endBalance = Number(data.equityEndBalances[i]);
      item.initBalance = Number(data.equityInitBalances[i]);

      this.data.ldfzTotal += item.endBalance;
    })
    this.data.nonCurrentLiabilities.forEach((item,i) => {
      item.endBalance = Number(data.equityEndBalances[i + length2]);
      item.initBalance = Number(data.equityInitBalances[i + length2]);

      this.data.fldfzTotal += item.endBalance;
    })
    this.data.ownerasset.forEach((item,i) => {
      item.endBalance = Number(data.equityEndBalances[i + length2 + length3]);
      item.initBalance = Number(data.equityInitBalances[i + length2 + length3]);

      this.data.ownerTotal += item.endBalance;
    })
    
    let total = this.data.ownerTotal + this.data.ldfzTotal + this.data.fldfzTotal;
    let ownerPercent = (this.data.ownerTotal / total*100).toFixed(2);
    let fzPercent = (this.data.ldfzTotal + this.data.fldfzTotal / total * 100).toFixed(2)
    this.data.optionData = [
      { value: this.data.ownerTotal, name: '所有者权益：' + ownerPercent+'%' },
      { value: this.data.ldfzTotal + this.data.fldfzTotal, name: '负债：' + fzPercent +'%' }
    ]

    this.data.chartList[0].items = this.data.currentAssets;
    this.data.chartList[1].items = this.data.nonCurrentAssets;
    this.data.chartList[2].items = this.data.currentLiabilities;
    this.data.chartList[3].items = this.data.nonCurrentLiabilities;
    this.data.chartList[4].items = this.data.ownerasset;

    // 获取组件 初始化图表
    this.selectComponent('#mychart-dom-bar').init((canvas, width, height) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      this.loadChart(chart);
      return chart;
    });

    wx.hideLoading()
    this.setData({
      currentAssets: this.data.currentAssets,
      nonCurrentAssets: this.data.nonCurrentAssets,
      currentLiabilities: this.data.currentLiabilities,
      nonCurrentLiabilities: this.data.nonCurrentLiabilities,
      ownerasset: this.data.ownerasset,
      chartList: this.data.chartList
    })
  },

  openChild(e){
    let index = e.currentTarget.dataset.index;
    this.data.chartList[index].lock = !this.data.chartList[index].lock;
    this.setData({
      chartList: this.data.chartList
    })
  },

  swich() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    if (this.data.chartOrform) {
      this.setData({
        chartOrform: false,
        fixedText: '图表',
        height: '0rpx'
      })
    } else {
      this.setData({
        chartOrform: true,
        fixedText: '列表',
        height: '380rpx'
      })
    }
  },

  loadChart(chart) {
    let silent = app.globalData.platform.toLowerCase() === 'android' ? true : false
    var option = {
      graphic: [
        {
          type: 'text',
          left: 'center',
          top: '47%',
          style: {
            text: '资产',
            textAlign: 'center',
            fill: '#8a8a8a',
            fontSize: 12
          },
        },
        {
          type: 'text',
          left: 'center',
          top: '55%',
          style: {
            text: common.currency(this.data.ownerTotal + this.data.ldfzTotal + this.data.fldfzTotal),
            textAlign: 'center',
            fontSize: 14
          }
        },
      ],
      series: [
        {
          type: 'pie',
          center: ['50%', '55%'],
          radius: ['45%', '70%'],
          data: this.data.optionData,
          minAngle: 3,
          silent: silent,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          },
          labelLine: {
            length: 5,
            length2: 5
          },
          color: ['#8ed22f', '#00c7da']
        }
      ]
    };
    chart.setOption(option);
  },

  downLoad() {
    let url = app.globalData.url + 'api/export/pdf/balancesheet?accountBookId=' + app.globalData.accountBookId + '&startPeriod=' + this.data.date + '&endPeriod=' + this.data.date + '&version=' + app.globalData.version + '&token=' + app.globalData.token
    common.download(url)
  }
})